package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.DeviceInfo;
import com.zzyl.nursing.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    /**
     * 根据房间 id 获取房间房型信息
     *
     * @param id
     */
    @Override
    public RoomVo getRoomById(Long id) {
        RoomVo roomVo = roomMapper.getRoomById(id);
        return roomVo;
    }
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询房间及关联的床位和设备
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
    /*    List<RoomVo> roomVos = roomMapper.getRoomsWithDeviceByFloorId(floorId);
            roomVos.forEach(roomVo -> {
                if (CollUtil.isEmpty(roomVos)){
                    throw new BaseException("调用mapperweikong");
                }
                List<DeviceInfo> deviceVos = roomVo.getDeviceVos();
                deviceVos.forEach(device -> {
                String s = (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, device.getIotId());
                    if (s!=null) {
                        List<DeviceData> deviceDataList = JSONUtil.toList(s, DeviceData.class);
                        device.setDeviceDataVos(deviceDataList);
                    }
                });
                roomVo.getBedVoList().forEach(bedVo -> {
                   bedVo.getDeviceVos().forEach(device->{
                       String s = (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, device.getIotId());
                       List<DeviceData> deviceDataList = JSONUtil.toList(s, DeviceData.class);
                       device.setDeviceDataVos(deviceDataList);
                   });
                });
            });
        return roomVos;*/
        //调用mapepr接口获取基础数据
        List<RoomVo> roomVos = roomMapper.getRoomsWithDeviceByFloorId(floorId);
        //  补全设备运行数据，从redis中获取
        roomVos.forEach(roomVo -> {
            //去得到房间得设备列表devicevos
            if (CollUtil.isEmpty(roomVos)) {
                throw new BaseException("调用Mapper失败");
            }

            List<DeviceInfo> deviceVos = roomVo.getDeviceVos();
            //获取每一个设备为设备添加上报数据最新的从redis获得
            deviceVos.forEach(device -> {
                String deviceDataListJSON = (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, device.getIotId());
                if (deviceDataListJSON != null) {
                    List<DeviceData> datalist = JSONUtil.toList(deviceDataListJSON, DeviceData.class);
                    device.setDeviceDataVos(datalist);
                }
            });

            // 获取到房间中的床位列表并遍历
            roomVo.getBedVoList().forEach(bedVo -> {
                // 获取到床位上的设备列表并遍历
                bedVo.getDeviceVos().forEach(deviceVo -> {
                    String deviceDataListJSON = (String)redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, deviceVo.getIotId());
                    List<DeviceData> deviceDataList = JSONUtil.toList(deviceDataListJSON,DeviceData.class);
                    // 将redis中的设备数据放入设备对象中
                    deviceVo.setDeviceDataVos(deviceDataList);
                });
            });
        });
        for (RoomVo roomVo : roomVos) {
            System.out.println(roomVo.getDeviceVos());
        }
        return roomVos;
    }
}
